define(function(require, exports, module) {
    var tencent = function() {
        this.point = null;
        this.marker = null;
        this.infowindow = null;
        this.map = null

    }
    tencent.prototype.load = function() {
        if (typeof(qq) == 'undefined') {
            var script = document.createElement("script");
            script.src = "http://map.qq.com/api/js?v=2.exp&key=JZSBZ-ZI4RF-HRCJY-JN3OW-V37KS-SOB4K&callback=wm.map.maps.init";
            document.body.appendChild(script);
        } else {
            this.init();
        }
    };
    tencent.prototype.init = function() {
        var self = this;
        self.point = new qq.maps.LatLng(wm.map.op.lat, wm.map.op.lng);
        self.map = new qq.maps.Map(document.getElementById("l-map"), {
            center: self.point,
            zoom: 15
        });

        self.infowindow = new qq.maps.InfoWindow({
            map: self.map,
            position: self.point,
            content: " <div style='width:200px'>原本位置 {0} ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(wm.map.op.adr)
        });
        self.marker = new qq.maps.Marker({
            position: self.point,
            draggable: true,
            map: self.map
        });
        qq.maps.event.addListener(self.map, 'drag', function() {
            var p = self.map.getCenter();
            self.marker.setPosition(p);

        });
        qq.maps.event.addListener(self.map, 'dragend', function() {
            var p = self.map.getCenter();
            self.infowindow.close();
            self.showAddress(p);
        });
        qq.maps.event.addListener(self.marker, 'dragend', function() {
            var p = self.marker.getPosition();
            self.showAddress(p);
            self.infowindow.close();

        });
        qq.maps.event.addListener(self.marker, 'click', function() {
            self.infowindow.open();
        });
        if (self.geocoder == null) {
            self.geocoder = new qq.maps.Geocoder();
        }
        ///同步infowindow
        qq.maps.event.addListener(self.marker, 'position_changed', function() {
            var p = self.marker.getPosition();
            self.infowindow.setPosition(p);
        });

        if (!wm.map.is_change) {
            self.infowindow.open();
        } else {
            self.search();
        }
        if (wm.map.is_default) {
            window.setTimeout(function() {
                self.locate();
            }, 1000);
        }
    };
    tencent.prototype.setval = function(result) {
        var self = this;
        var p = result.detail.location,
            addr = result.detail.address;
        self.marker.setPosition(p);
        self.map.panTo(p);
        var el = wm.map.el;
        el.lat.val(p.lat);
        el.lng.val(p.lng);
        el.suggestId.val(addr);
    };

    tencent.prototype.locate = function() {
        var self = this;
        navigator.geolocation.getCurrentPosition(function(position) {
            var p = new qq.maps.LatLng(position.coords.latitude, position.coords.longitude);
            self.map.panTo(p)
            self.marker.setPosition(p);
            self.infowindow.setContent("<div style='width:200px'>定位成功这是你当前的位置!,移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!</div>");
            self.infowindow.open();
            console.log(p);
            self.rgeocoder().getAddress(p);
        });
    };
    tencent.prototype.search = function() {
        var self = this;
        var addr = wm.map.el.suggestId.val();
        addr = addr.length > 0 ? addr : wm.map.op.adr;
        self.geocoder.getLocation(addr);
        self.geocoder.setComplete(function(result) {
            self.setval(result);
            self.infowindow.open();
            self.infowindow.setContent("<div style='width:200px'>搜索位置 {0} ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(result.detail.address))
        })
    };
    tencent.prototype.showAddress = function(p) {
        var self = this;
        self.geocoder.getAddress(p);
        self.geocoder.setComplete(function(result) {
            self.setval(result);
            self.infowindow.setContent("<div style='width:200px'>标注位置 {0} ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(result.detail.address))
        })
    };

    return tencent;
});
